package com.yuwenxin.servlet.controller.user;

import com.yuwenxin.bean.User;
import com.yuwenxin.bean.queryModel.UserQueryModel;
import com.yuwenxin.core.PasswordEncodeUtil;
import com.yuwenxin.dao.UserDao;
import com.yuwenxin.dao.impl.UserDaoImpl;
import com.yuwenxin.service.UserService;
import com.yuwenxin.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RegisterController extends HttpServlet {
    @Override
    public void init() throws ServletException {
        System.out.println("RegisterController initial...");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        通过Filter来解决全站的编码问题---------------------------------------
//        resp.setHeader("Content-type", "text/html;charset=utf-8");
//        resp.setCharacterEncoding("utf-8");

        String name = req.getParameter("name");
        String password = req.getParameter("password");
        String passwordConfirm = req.getParameter("passwordConfirm");
        String email = req.getParameter("email");
        String sex = req.getParameter("sex");

        UserQueryModel form = new UserQueryModel();

        if (!password.equals(passwordConfirm)){
            req.setAttribute("DBMes", "密码确认错误");
            req.setAttribute("form",form);
            req.getRequestDispatcher("/register.jsp").forward(req,resp);
            return;
        }
        System.out.println("密码重复检测...");

        form.setName(name);
        form.setPassword(password);
        form.setPasswordConfirm(passwordConfirm);
        form.setEmail(email);
        form.setSex(sex);
        System.out.println("form表单赋值...");

        if (!form.regist_validate(password)){
            req.setAttribute("form",form);
            req.getRequestDispatcher("/register.jsp").forward(req,resp);
            return;
        }
        System.out.println("数据检测...");

        User user;
        try {
            //dao.insert(user);
            System.out.println("try代码块进入...");
            UserService service = new UserServiceImpl();
            System.out.println("创建一个service对象...");
            user = service.register(form);
            System.out.println("调用service中...");
            if (user == null){
                req.setAttribute("DBMes", "用户已经存在");
                req.setAttribute("form", form);
                req.getRequestDispatcher("/register.jsp").forward(req,resp);
                return;
            }

            req.getSession().setAttribute("user",user);
//            req.getSession().setAttribute("password", password);
            autoLogin(req, resp, name, password);

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            resp.getWriter().println("注册成功，3秒后跳转...");
            resp.setHeader("refresh","3;url=regSuccess.jsp");
        }


    }

    static void autoLogin(HttpServletRequest req, HttpServletResponse resp, String name, String password) {
        String autoLogin = req.getParameter("autoLogin");
        if (autoLogin == null){
            Cookie cookie = new Cookie("autoLogin", name + "-" + PasswordEncodeUtil.encodePassword(password));
//            Cookie cookie1 = new Cookie("name",name);
            cookie.setMaxAge(60*60*24*31*3);
            cookie.setPath(req.getContextPath());
//            cookie1.setMaxAge(60*60*24*31*3);
//            cookie1.setPath(req.getContextPath());
            resp.addCookie(cookie);
//            resp.addCookie(cookie1);
        }
    }

    @Override
    public void destroy() {
        System.out.println("RegisterController destroy...");
    }
}
